<?PHP
include("../inc/config.php");

if($_GET['action'] == "save") {
	// Salvo i settaggi
	
	$myField = 'CONFIG>TABLES>TABLE.name='.$_SESSION['configuring_table'].'>FIELDS>FIELD.name='.$_GET['field'];
	$_CONF->remove($myField . ">VIRTUALCROSS.childNodes");
	
	foreach($_POST as $key => $value) {
		if(strpos($key, 'vcross_') !== false && strpos($key, '_value') === false) {
			$_CONF->setValue($myField . '>VIRTUALCROSS>DATA.label='.$value.'.label', $value);
			$_CONF->setValue($myField . '>VIRTUALCROSS>DATA.label='.$value.'.value', $_POST[str_replace("_label", "_value", $key)]);
		}
	}
	$_CONF->saveBuiltXML();
}

?>
<html>
<head>
	<title>Config Cross Field Virtual</title>
	<style type="text/css">@import '../css/common.css';</style>
	<script type="text/javascript" src="../js/common.js"></script>
	<script>
		var VF = {
			save: false, 
			index: 0,
			
			init: function() 
			{
				this.index = $('virtualCrossConf').down(0).childElements().length;
				this.checkStatus();
			},
			
			addVCrossRow: function() 
			{
				this.index++;
				var id = this.index;
				var newRow = Builder.node('tr', [
					Builder.node('td', [
						Builder.node('input', { type: 'button', value: 'remove', onclick: 'VF.removeVCrossRow(this)'})
					]),
					Builder.node('td', [
						Builder.node('input', {type: 'text', name: 'vcross_'+id+'_label'})
					]),
					Builder.node('td', [
						Builder.node('input', {type: 'text', name: 'vcross_'+id+'_value'})
					])
				]);
				
				if($('virtualCrossConf').down(0).nodeName == 'TBODY')
					$('virtualCrossConf').down(0).appendChild(newRow);
				else 
					$('virtualCrossConf').appendChild(newRow);
				this.addSaveButton();
				this.checkStatus();
				this.save = true;
			},
			
			addSaveButton: function() 
			{
				if($('SAVE_btn') == null) {
					var btn = Builder.node('input', {id: 'SAVE_btn', type: 'button', value: 'SAVE'});
					btn.setStyle({
						'background'	: 'green',
						'color'			: '#fff',
						'fontWeight'	: 'bold',
						'border'		: '0',
						'padding'		: '4px',
						'margin'		: '0 5px'
					});
					btn.onclick = function() {
						$('virtualCrossForm').submit();
					}
					$('vf_controller').appendChild(btn);
				}
			},
			
			removeVCrossRow: function(e) 
			{
				Element.remove(e.up(1));
				this.checkStatus();
				this.addSaveButton();
			},
			
			checkStatus: function() 
			{
				if($('virtualCrossConf').down(0).nodeName == 'TBODY')
					var tbody = $('virtualCrossConf').down(0);
				else 
					var tbody = $('virtualCrossConf');
				if(tbody.childElements().length > 2) {
					$('vf_headers').show();
					$('no_vf').hide();
				} else {
					$('vf_headers').hide();
					$('no_vf').show();
				}
			}
		}
	</script>
</head>
<body>
	
	<?php
	$vcrossNodes = $_CONF->get('CONFIG>TABLES>TABLE.name='.$_SESSION['configuring_table'].'>FIELDS>FIELD.name='.$_GET['field'].'>VIRTUALCROSS.childNodes');
	?>
	<style>
		body {position: relative}
		h1 {margin: 0; padding: 6px}
		.vf_container {overflow: auto; height: 305px; padding: 5px; position: relative}
		.vf_controller {position: absolute; bottom: 10px; left: 4px; width: 482px; text-align: right}
	</style>
	<h1>Impostazione virtual-fields</h1>
	<div class="vf_container">
		
		<form id="virtualCrossForm" action="?action=save&field=<?=$_GET['field']?>&table=<?=$_SESSION['configuring_table']?>" method="post">
		<table id="virtualCrossConf" cellspacing="0" cellpadding="4" width="100%">
		<tr id="vf_headers" bgcolor="#f2f2f2"><th></th><th align="left"><strong>Nome</strong></th><th align="left"><strong>Valore</strong></th></tr>
		<tr id="no_vf"><td colspan="3">Non ci sono valori virtuali impostati.</td></tr>
		<?
		if($vcrossNodes !== false) {
			// Ci sono dei virtual cross values settati, li mostro
			$i=0;
			foreach($vcrossNodes as $node) {
				echo '<tr><td><input type="button" value="remove" onclick="VF.removeVCrossRow(this);"></td><td><input name="vcross_'.$i.'_label" type="text" value="'.$_CONF->get('DATA.label', $node).'"></td><td><input name="vcross_'.$i.'_value" type="text" value="'.$_CONF->get('DATA.value', $node).'"></td></tr>';
				$i++;
			}
		}
		?>
		</table>
		</form>
	</div>
	
	<div class="vf_controller" id="vf_controller">
		<? if($_GET['action'] == "save") echo '<h3 style="float:left; background: green; padding: 4px 8px; color: #FFF; font-weight: bold">Salvato</h3>'; ?>
		<input type="button" value="R" onclick="window.location.reload();"> &nbsp; <input type="button" name="addButton" value="AGGIUNGI" class="buttonLeft" onClick="VF.addVCrossRow();" />
	</div>
	
	<script>
		VF.init();
	</script>
	
</body>
</html>
